<?php
namespace Admin\Model;

class RuleModel
{   
    /**
     * 获取所有的权限列表
     * @return [type] [description]
     */
    public function getRules()
    {
        return $this->getAll();
    }

    /**
     * 从表里获取所有
     * @return [type]
     */
    private function getAllFromTable()
    {
        $where = [
            'parent_id' => 0,
        ];
        $datas = M('admin_rule')->where($where)->limit(false)->lists();

        $childs = M('admin_rule')->where('parent_id != 0')->select();
        foreach ($childs as $value) {
            $datas[$value['parent_id']]['childs'][] = $value;
        }

        return $datas;
    }

    /**
     * 从缓存中取
     * @return [type]
     */
    private function getAll()
    {
        $rules = S('rules');
        if (!$rules) {
            $rules = $this->getAllFromTable();
            S('rules', $rules);
        }

        return $rules;
    }
}
